home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / point.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  5KB  |  229 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "gltk.h"
  29.  
  30. #define CI_RED TK_RED
  31. #define CI_ANTI_ALIAS_GREEN 16
  32. #define CI_ANTI_ALIAS_YELLOW 32
  33. #define CI_ANTI_ALIAS_RED 48
  34.  
  35. GLenum rgb, doubleBuffer, directRender, windType;
  36. GLint windW, windH;
  37.  
  38. GLenum mode;
  39. GLint size;
  40. float point[3] =
  41. {
  42.   1.0, 1.0, 0.0
  43. };
  44.  
  45. static void Init(void)
  46. {
  47.   GLint i;
  48.  
  49.   glClearColor(0.0, 0.0, 0.0, 0.0);
  50.  
  51.   glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  52.  
  53.   if (!rgb) {
  54.     for (i = 0; i < 16; i++) {
  55.       tkSetOneColor(i + CI_ANTI_ALIAS_RED, i / 15.0, 0.0, 0.0);
  56.       tkSetOneColor(i + CI_ANTI_ALIAS_YELLOW, i / 15.0, i / 15.0, 0.0);
  57.       tkSetOneColor(i + CI_ANTI_ALIAS_GREEN, 0.0, i / 15.0, 0.0);
  58.     }
  59.   }
  60.  
  61.   mode = GL_FALSE;
  62.   size = 1;
  63. }
  64.  
  65. static void Reshape(int width, int height)
  66. {
  67.  
  68.   windW = (GLint) width;
  69.   windH = (GLint) height;
  70.  
  71.   glViewport(0, 0, width, height);
  72.  
  73.   glMatrixMode(GL_PROJECTION);
  74.   glLoadIdentity();
  75.   gluOrtho2D(-windW / 2, windW / 2, -windH / 2, windH / 2);
  76.   glMatrixMode(GL_MODELVIEW);
  77. }
  78.  
  79. static GLenum Key(int key, GLenum mask)
  80. {
  81.  
  82.   switch (key) {
  83.     case TK_ESCAPE:
  84.       tkQuit();
  85.     case TK_1:
  86.       mode = !mode;
  87.       break;
  88.     case TK_W:
  89.       size++;
  90.       break;
  91.     case TK_w:
  92.       size--;
  93.       if (size < 1) {
  94.     size = 1;
  95.       }
  96.       break;
  97.     case TK_LEFT:
  98.       point[0] -= 0.25;
  99.       break;
  100.     case TK_RIGHT:
  101.       point[0] += 0.25;
  102.       break;
  103.     case TK_UP:
  104.       point[1] += 0.25;
  105.       break;
  106.     case TK_DOWN:
  107.       point[1] -= 0.25;
  108.       break;
  109.     default:
  110.       return GL_FALSE;
  111.   }
  112.   return GL_TRUE;
  113. }
  114.  
  115. static void Draw(void)
  116. {
  117.  
  118.   glClear(GL_COLOR_BUFFER_BIT);
  119.  
  120.   TK_SETCOLOR(windType, TK_YELLOW);
  121.   glBegin(GL_LINE_STRIP);
  122.   glVertex2f(-windW / 2, 0);
  123.   glVertex2f(windW / 2, 0);
  124.   glEnd();
  125.   glBegin(GL_LINE_STRIP);
  126.   glVertex2f(0, -windH / 2);
  127.   glVertex2f(0, windH / 2);
  128.   glEnd();
  129.  
  130.   if (mode) {
  131.     glEnable(GL_BLEND);
  132.     glEnable(GL_POINT_SMOOTH);
  133.   }
  134.   else {
  135.     glDisable(GL_BLEND);
  136.     glDisable(GL_POINT_SMOOTH);
  137.   }
  138.  
  139.   glPointSize(size);
  140.   if (mode) {
  141.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_ANTI_ALIAS_RED);
  142.   }
  143.   else {
  144.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_RED);
  145.   }
  146.   glBegin(GL_POINTS);
  147.   glVertex3fv(point);
  148.   glEnd();
  149.  
  150.   glDisable(GL_POINT_SMOOTH);
  151.   glDisable(GL_BLEND);
  152.  
  153.   glPointSize(1);
  154.   TK_SETCOLOR(windType, TK_GREEN);
  155.   glBegin(GL_POINTS);
  156.   glVertex3fv(point);
  157.   glEnd();
  158.  
  159.   glFlush();
  160.  
  161.   if (doubleBuffer) {
  162.     tkSwapBuffers();
  163.   }
  164. }
  165.  
  166. static GLenum Args(int argc, char **argv)
  167. {
  168.   GLint i;
  169.  
  170.   rgb = GL_TRUE;
  171.   doubleBuffer = GL_FALSE;
  172.   directRender = GL_TRUE;
  173.  
  174.   for (i = 1; i < argc; i++) {
  175.     if (strcmp(argv[i], "-ci") == 0) {
  176.       rgb = GL_FALSE;
  177.     }
  178.     else if (strcmp(argv[i], "-rgb") == 0) {
  179.       rgb = GL_TRUE;
  180.     }
  181.     else if (strcmp(argv[i], "-sb") == 0) {
  182.       doubleBuffer = GL_FALSE;
  183.     }
  184.     else if (strcmp(argv[i], "-db") == 0) {
  185.       doubleBuffer = GL_TRUE;
  186.     }
  187.     else if (strcmp(argv[i], "-dr") == 0) {
  188.       directRender = GL_TRUE;
  189.     }
  190.     else if (strcmp(argv[i], "-ir") == 0) {
  191.       directRender = GL_FALSE;
  192.     }
  193.     else {
  194.       printf("%s (Bad option).\n", argv[i]);
  195.       return GL_FALSE;
  196.     }
  197.   }
  198.   return GL_TRUE;
  199. }
  200.  
  201. void main(int argc, char **argv)
  202. {
  203.  
  204.   if (Args(argc, argv) == GL_FALSE) {
  205.     tkQuit();
  206.   }
  207.  
  208.   windW = 300;
  209.   windH = 300;
  210.   tkInitPosition(0, 0, windW, windH);
  211.  
  212.   windType = (rgb) ? TK_RGB : TK_INDEX;
  213.   windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  214.   windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  215.   tkInitDisplayMode(windType);
  216.  
  217.   if (tkInitWindow("Point Test") == GL_FALSE) {
  218.     tkQuit();
  219.   }
  220.  
  221.   Init();
  222.  
  223.   tkExposeFunc(Reshape);
  224.   tkReshapeFunc(Reshape);
  225.   tkKeyDownFunc(Key);
  226.   tkDisplayFunc(Draw);
  227.   tkExec();
  228. }
  229.